package sk.fiit.icp.data;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class SettingsDaoImpl implements SettingsDao {

	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate( sessionFactory );
	}

	@Transactional
	public Settings findById(int id) {
		return (Settings) hibernateTemplate.find( "from Settings AS s WHERE s.idsettings = ?", id ).get( 0 );
	}

	@Transactional(readOnly = false)
	public void persist(Settings e) {
		hibernateTemplate.merge( e );
	}

	@Transactional(readOnly = false)
	public void update(Settings e) {
		hibernateTemplate.merge( "Settings", e );
	}

	@Transactional
	public Settings findByUser(Users u) {
		@SuppressWarnings("unchecked")
		List<Settings> s = hibernateTemplate.find( "from Settings AS s fetch all properties WHERE s.users = ?", u );
		return (Settings) (s.isEmpty() ? null : s.get( 0 ));
	}

}
